﻿'**********************************************************************************************************************************
'HISTORY & ACKNOWLEDGEMENT
'The h20tulkit is a part of the PhD work on STATISTICAL MODELS & TOOLKIT FOR WATER QUALITY MODELLING OF POLLUTED RIVERS 
'by Amos T. Kabo-bah. This work was supervised by Prof Xie Yuebo of the State Key 
'Laboratories under the College of Hydrology and Water Resources of Hohai University, China.
'This work is therefore the efforts of Amos T. Kabo-bah under the kind guidannce and supervision of Prof Xie Yuebo.
'The graphing tool in this code was adopted from JChampion from [http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET]
'The building of the setup files of the software were obtained from inno setup [http://www.jrsoftware.org/isinfo.php]
'Ico for setup identification was downloaded free from [http://www.iconarchive.com/show/mushrooms-icons-by-madoyster.html]
'Other useful online contributors that supported the building of the setup files included Johhny Coder post of [http://johnnycoder.com/blog/2008/07/28/getting-started-with-inno-setup/]
'The support from colleague PhD student - Mark Y. Amo-Boateng greatly enhanced the code, and the resolution of bugs. 
'The support of my colleague students - Lui Hui, Doris (Lv Tianhui), Cindy (Xu Jie), Qiao Wa, Yu Jing (Fish) and Dong Weiling is also appreciated. 
'The moral support and counsel from my family - my wife - Kamila Kabo-bah and son - Gabriel Kabo-bah are duely appreciated.
'ABOVE ALL, the GRACE, MERCY and LOVE from my LORD and SAVIOUR - JESUS CHRIST surpasses ALL UNDERSTANDING!
'**********************************************************************************************************************************
'FOR INQUIRES 
'Contact me at kabobah@hhu.edu.cn or kabo-bah@greenwaterhut.org
'**********************************************************************************************************************************


'CRITICAL FILES FOR THE CODE
Imports System.Data.OleDb
Imports System.Math
Imports DGVPrinterHelper
Imports ZedGraph
Imports System
Imports System.IO

'BEGINNING OF CODE

Public Class h20tulkit
    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    End Sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    End Sub
    Private Sub dg_Preview_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
    End Sub
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Return
    End Sub

    Private Sub Button_Computation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Computation.Click
        If dgv1.CurrentCell Is Nothing Then
            MessageBox.Show("Import your excel data first")
            Return
        End If
        If cbBOD.Checked = False And cbTP.Checked = False And cbNH3.Checked = False Then
            MessageBox.Show("Error, select prediction variable")
            Return
        ElseIf cbBOD.Checked = True And cbTP.Checked = True And cbNH3.Checked = False Then
            MessageBox.Show("Error, select only ONE prediction variable")
            Return
        ElseIf cbBOD.Checked = False And cbTP.Checked = True And cbNH3.Checked = True Then
            MessageBox.Show("Error, select only ONE prediction variable")
            Return
        ElseIf cbBOD.Checked = True And cbTP.Checked = False And cbNH3.Checked = True Then
            MessageBox.Show("Error, select only ONE prediction variable")
            Return
        End If
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = dgv1.Rows.Count - 1
        Dim min As Integer = 0
        Dim max As Integer = dgv1.Rows.Count - 1
        Try
            '-----------------------------PREPARATION OF COMPUTATION------------------------------------
            Dim calc_col As Integer = dgv1.Columns.Count - 1
            For i = 0 To dgv1.Rows.Count - 1 Step 1

                '-------------------------PROGRESS/STATUS BAR----------------------------------
                ProgressBar1.Visible = True
                'frmProgress.ProgressBar1.Value = i
                LbProgress.Visible = True
                ProgressBar1.Value = i
                LbProgress.Text = i / (max - min) * 100 & "% Complete"

                '-------------------TP Algorithms and Data formating --------------------------------------
                Dim eqn As Double
                Dim pH As Double = dgv1.Rows(i).Cells(0).Value
                Dim COD As Double = dgv1.Rows(i).Cells(1).Value
                Dim NH_3_N As Double = dgv1.Rows(i).Cells(2).Value


                '------------BOD Algorithms and Data formating ---------------------------------------------
                Dim eqn_1 As Double
                Dim pH_b As Double = dgv1.Rows(i).Cells(0).Value
                Dim DO_b As Double = dgv1.Rows(i).Cells(1).Value
                Dim COD_b As Double = dgv1.Rows(i).Cells(2).Value
                Dim TP_b As Double = dgv1.Rows(i).Cells(3).Value
                Dim NH_3_N_b As Double = dgv1.Rows(i).Cells(4).Value
                'Dim TN As Double = dgv1.Rows(i).Cells(5).Value

                '------------Ammonia-Nitrogen Algorithms and Data formating--------------
                Dim eqn_2 As Double
                Dim TP_a As Double = dgv1.Rows(i).Cells(0).Value
                Dim TN_a As Double = dgv1.Rows(i).Cells(1).Value
                Dim WT_a As Double = dgv1.Rows(i).Cells(2).Value
                Dim T_a As Double = dgv1.Rows(i).Cells(3).Value
                Dim DO_a As Double = dgv1.Rows(i).Cells(4).Value
                Dim COD_a As Double = dgv1.Rows(i).Cells(5).Value

                '------------------CHECK for Selection of WQ Variable and Name column heading---
                If cbTP.Checked Then
                    dgv1.Columns(calc_col).HeaderText = "Calc Tp"
                ElseIf cbBOD.Checked Then
                    dgv1.Columns(calc_col).HeaderText = "Calc BOD5"
                ElseIf cbNH3.Checked Then
                    dgv1.Columns(calc_col).HeaderText = "Calc NH3_N"
                End If

                '-------------------------------CALCULATION FOR TP PREDICTION----------------------

                If cbTP.Checked And cbNH3_p.Checked And cbCOD_p.Checked And cbPH_p.Checked Then
                    eqn = 2.73 - (0.377 * pH) + ((0.009304) * (pH * NH_3_N)) + ((0.001186) * (pH * COD))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbNH3_p.Checked Then
                    eqn = (0.0028025) + (0.093879) * NH_3_N
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbNH3_p.Checked And cbCOD_p.Checked Then

                    eqn = 0.453 + (0.0011845) * NH_3_N * COD
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbPH_p.Checked And cbCOD_p.Checked Then

                    eqn = 0.171 + (0.0027797) * pH * COD
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbPH_p.Checked And cbNH3_p.Checked Then
                    eqn = 3.248 - 0.447 * pH + (0.013552) * pH * NH_3_N
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                    '------------------------------CALCULATION FOR BOD PREDICTION-------------------------------------

                ElseIf cbBOD.Checked And cbBOD_p.Checked And cbNH3_p.Checked And cbTP_p.Checked Then
                    eqn_1 = 9.31452 + 0.0126097 * COD * NH_3_N_b + 6.57477 * TP_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked Then
                    eqn_1 = 12.5032 + 0.0145457 * COD_b * NH_3_N_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1

                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbTP_p.Checked And cbPH_p.Checked Then
                    eqn_1 = -9.39783 + 0.0125041 * COD_b * NH_3_N_b + 6.19133 * TP_b + 2.63608 * pH_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1

                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbDO_p.Checked Then
                    eqn_1 = 18.4417 + 0.0141608 * COD_b * NH_3_N_b - 0.0656279 * DO_b * COD_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                ElseIf cbBOD.Checked And cbBOD_p.Checked And cbNH3_p.Checked And cbDO_p.Checked And cbPH_p.Checked Then
                    eqn_1 = -16.8157 + 0.0142863 * COD_b * NH_3_N_b - 0.144308 * DO_b * NH_3_N_b + 4.36124 * pH_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbDO_p.Checked And cbTP_p.Checked Then
                    eqn_1 = 11.4174 + 0.0146564 * COD_b * NH_3_N_b + 0.851954 * DO_b * TP_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                    '--------------------------Calculation of Ammonia-Nitrogen Prediction-----------
                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbT_p.Checked And cbTrans_p.Checked Then
                    eqn_2 = -6.94255 + 0.91888 * Sqrt((TP_a) * (TN_a)) - 304.222 * 1 / ((T_a) * (WT_a)) + 3.86231 * Sqrt((TN_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbTrans_p.Checked Then
                    eqn_2 = -8.02138 + 0.941416 * Sqrt((TP_a) * (TN_a)) + 3.77342 * Sqrt((TN_a)) + 0.0311467 * (T_a)
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbTP_p.Checked And cbTN_p.Checked And cbTrans_p.Checked And cbDO_p.Checked Then
                    eqn_2 = -8.32092 + 0.952719 * Sqrt((TP_a) * (TN_a)) + 3.7428 * Sqrt((TN_a)) + 0.151695 * Sqrt((T_a) * (DO_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbT_p.Checked Then
                    eqn_2 = -6.49137 + 0.917595 * Sqrt((TP_a) * (TN_a)) - (315.105 * 1 / ((T_a) * (WT_a))) + 3.93491 * Sqrt((TN_a)) - (0.00224571 * COD_a * WT_a)
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2


                ElseIf cbNH3.Checked And cbT_p.Checked And cbTN_p.Checked And cbTP_p.Checked Then
                    eqn_2 = 2.36176 - 0.326788 * (WT_a) + 0.73978 * Sqrt((TN_a) * (WT_a)) + 0.682419 * Sqrt((TP_a) * (TN_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbTrans_p.Checked And cbT_p.Checked And cbCOD_p.Checked And cbDO_p.Checked Then
                    eqn_2 = -6.21001 + 0.90686 * Sqrt((TP_a) * (TN_a)) - 312.594 * 1 / ((T_a) * (WT_a)) + 3.93318 * Sqrt((TN_a)) - 0.666498 * Log10((COD_a) * (DO_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTN_p.Checked And cbT_p.Checked And cbTP_p.Checked Then
                    eqn_2 = -4.63988 + 4.65421 * Sqrt((TN_a)) - 0.0791775 * (WT_a) - 0.292869 * (1 / (TP_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                    '-----------------Check for ERRORS in COMPUTATION -------------------------------
                Else
                    MessageBox.Show("There are no computations option for selected variables or ensure that you have selected your parameters!")
                    Return
                End If
                'Return
            Next

            MsgBox("Calculation Complete")
            ProgressBar1.Value = min
            ProgressBar1.Visible = False
            LbProgress.Visible = False
            LbProgress.Text = ""

            frmProgress.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        '------------------------------------------------plot graphs, TP, BOD, TN, COD, Ammonium-Nitrogen -------------------------------
        If cbTP.Checked Then
            GraphLabel.Visible = True
            GraphLabel.Text = "Prediction of TP Model"
            PlotDataGridGraph(4, 5, True)
            'CopyDataGridColumn(dgv1, dgv2, 4, 0, True, "Obs")
            'CopyDataGridColumn(dgv1, dgv2, 5, 0, True, "Calc")
        ElseIf cbBOD.Checked Then
            GraphLabel.Visible = True
            GraphLabel.Text = "Prediction of BOD Model"
            PlotDataGridGraph(5, 6)
            'CopyDataGridColumn(dgv1, dgv2, 5, 0, True, "Obs")
            'CopyDataGridColumn(dgv1, dgv2, 6, 0, True, "Calc")
        ElseIf cbNH3.Checked Then
            GraphLabel.Visible = True
            GraphLabel.Text = "Prediction of NH3-N Model"
            PlotDataGridGraph(6, 7)
            'CopyDataGridColumn(dgv1, dgv2, 6, 0, True, "Obs")
            'CopyDataGridColumn(dgv1, dgv2, 7, 0, True, "Calc")

            Return
        End If
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim OpenFileDialog As New OpenFileDialog
        If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            '    'Dim Pathname As String = OpenFileDialog.FileName
            Dim sFileName As String = OpenFileDialog.FileName
            Dim connectionStringTemplate As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"""
            Dim connectionString As String = String.Format(connectionStringTemplate, sFileName)
            Dim sqlSelect As String = "SELECT * FROM [Sheet1$];"
            ' Load the Excel worksheet into a DataTable
            Dim workbook As DataSet = New DataSet()
            Dim excelAdapter As System.Data.Common.DataAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlSelect, connectionString)
            Try
                excelAdapter.Fill(workbook)
                Dim worksheet As DataTable = workbook.Tables(0)
                dgv1.DataSource = worksheet
                dgv1.Columns.Add("calc_col", "")
                Me.Refresh()
            Catch
                'MANAGE EXCEPTION
            End Try
        End If
    End Sub

    Private Sub graph1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub

    'Dim myPane As GraphPane = zg1.GraphPane

    Public Function PlotDataGridGraph(ByVal obs As Integer, ByVal calc As Integer, Optional ByVal x_axis As Integer = -1, Optional ByVal plotSentivity As Boolean = False, Optional ByVal PlotCalc As Boolean = False)
        Dim myPane As GraphPane = zg1.GraphPane

        ' Set the titles and axis labels

        myPane.Title.Text = "Calculated vrs Observed"
        myPane.XAxis.Title.Text = "Points"
        myPane.YAxis.Title.Text = "Concentrations (mg/l)"
        'myPane.Y2Axis.Title.Text = "Observed"
        'myPane.Y3Axis.title.text = "Scenario Run  Analysis"

        ' Make up some data points based on the Sine function
        Dim list As New PointPairList
        Dim list2 As New PointPairList
        Dim list3 As New PointPairList
        Dim i As Integer, x As Double, y As Double, y2 As Double, y3 As Double

        Dim obs_col As Integer = obs
        Dim calc_col As Integer = calc
        Dim sen_col As Integer = sen_col

        Dim row_cnt As Integer = dgv1.Rows.Count

        Try

            For i = 0 To row_cnt - 1
                'default x is the row number

                If dgv1.Rows(i).Cells(obs_col).Value Is Nothing Then

                Else

                    If x_axis = -1 Then
                        x = i
                    Else
                        x = dgv1.Rows(i).Cells(x_axis).Value
                    End If
                    Dim col_cnt As Integer = dgv1.Columns.Count - 1
                    'For k = 0 To col_cnt - 1

                    'Next
                    y = dgv1.Rows(i).Cells(obs_col).Value
                    y2 = dgv1.Rows(i).Cells(calc_col).Value
                    y3 = dgv1.Rows(i).Cells(sen_col).Value
                End If

                '----- ------------------------------------------
                'x = i * 5.0

                ''y = DataGridView.Columns("pH").value
                'y = Math.Sin(i * Math.PI / 15.0) * 16.0
                'y2 = y * 13.5
                '-------------------------------------------------

                list.Add(x, y)
                list2.Add(x, y2)
                list3.Add(x, y3)
            Next i

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

        If myPane.CurveList.Count > 0 Then myPane.CurveList.Clear()


        ' Generate a red curve with diamond symbols, and "Alpha" in the legend
        Dim myCurve As LineItem
        'If PlotCalc = True Then
        myCurve = myPane.AddCurve("Observed", list, Color.Green, SymbolType.Star)
        ' Fill the symbols with white
        myCurve.Symbol.Fill = New Fill(Color.White)

        ' Generate a blue curve with circle symbols, and "Beta" in the legend
        myCurve = myPane.AddCurve("Calculated", list2, Color.Blue, SymbolType.Triangle)
        ' Fill the symbols with white
        myCurve.Symbol.Fill = New Fill(Color.White)
        ' Associate this curve with the Y2 axis

        'End If


        If plotSentivity = True Then
            'If BtStimulation.Enabled = True Then
            'myCurve = myPane.AddCurve("Observed", list, Color.Red, SymbolType.Diamond)
            '' Fill the symbols with white
            'myCurve.Symbol.Fill = New Fill(Color.White)
            myCurve = myPane.AddCurve("Scenario", list3, Color.Chocolate, SymbolType.Diamond)
            ' Fill the symbols with white
            myCurve.Symbol.Fill = New Fill(Color.White)
            myCurve.IsY2Axis = True
        End If




        ' Show the x axis grid
        myPane.XAxis.MajorGrid.IsVisible = True

        ' Make the Y axis scale red
        myPane.YAxis.Scale.FontSpec.FontColor = Color.Red
        myPane.YAxis.Title.FontSpec.FontColor = Color.Red
        ' turn off the opposite tics so the Y tics don't show up on the Y2 axis
        myPane.YAxis.MajorTic.IsOpposite = False
        myPane.YAxis.MinorTic.IsOpposite = False
        ' Don't display the Y zero line
        myPane.YAxis.MajorGrid.IsZeroLine = False
        ' Align the Y axis labels so they are flush to the axis
        myPane.YAxis.Scale.Align = AlignP.Inside
        ' Manually set the axis range
        myPane.YAxis.Scale.Min = 0
        myPane.YAxis.Scale.Max = 100
        ' Enable the Y2 axis display
        myPane.Y2Axis.IsVisible = False
        ' Make the Y2 axis scale blue
        myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue
        myPane.Y2Axis.Title.FontSpec.FontColor = Color.Blue
        ' turn off the opposite tics so the Y2 tics don't show up on the Y axis
        myPane.Y2Axis.MajorTic.IsOpposite = False
        myPane.Y2Axis.MinorTic.IsOpposite = False
        ' Display the Y2 axis grid lines
        myPane.Y2Axis.MajorGrid.IsVisible = True
        ' Align the Y2 axis labels so they are flush to the axis
        myPane.Y2Axis.Scale.Align = AlignP.Inside

        ' Fill the axis background with a gradient
        myPane.Chart.Fill = New Fill(Color.White, Color.LightGray, 45.0F)

        ' Add a text box with instructions
        Dim text As New TextObj( _
     "Zoom: left mouse & drag" & Chr(10) & "Pan: middle mouse & drag" & Chr(10) & "Context Menu: right mouse", _
     0.05F, 0.95F, CoordType.ChartFraction, AlignH.Left, AlignV.Bottom)
        text.FontSpec.StringAlignment = StringAlignment.Near
        myPane.GraphObjList.Add(text)

        ' Enable scrollbars if needed
        zg1.IsShowHScrollBar = True
        zg1.IsShowVScrollBar = True
        zg1.IsAutoScrollRange = True
        zg1.IsScrollY2 = True

        zg1.IsShowPointValues = True

        ' Size the control to fit the window
        SetSize()

        ' Tell ZedGraph to calculate the axis ranges
        ' Note that you MUST call this after enabling IsAutoScrollRange, since AxisChange() sets
        ' up the proper scrolling parameters
        zg1.AxisChange()
        ' Make sure the Graph gets redrawn
        zg1.Invalidate()
    End Function

    Private Sub Form1_load(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.F1 Then
            System.Windows.Forms.Help.ShowHelp(Me, "Help.chm", HelpNavigator.TableOfContents)

        End If
    End Sub

    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        'Tooltip 
        Me.tt.SetToolTip(Me.calculate, "Perform Calculation")
        Me.tt.SetToolTip(Me.Load_file, "Import Excel Doc")
        Me.tt.SetToolTip(Me.Prt_fast, "Print Table in Worksheet")
        Me.tt.SetToolTip(Me.h20tulkit_help, "Help")
        Me.tt.SetToolTip(Me.Reset, "Reset to default settings")
        Me.tt.SetToolTip(Me.Graph, "Show Graph")
        Me.tt.SetToolTip(Me.Model_Report, "Print Model Report")
        Me.tt.SetToolTip(Me.h20tulkit_help, "Help")
        Me.tt.SetToolTip(Me.cbBOD, "Prediction of BOD5 based on TP, TN, T, Transparency, DO or COD")
        Me.tt.SetToolTip(Me.cbTP, "Prediction of TP based on pH, NH3-N and COD")
        Me.tt.SetToolTip(Me.cbNH3, "Prediction of NH3-N based on TP, TN, T, Transparency, DO or COD")
        Me.tt.SetToolTip(Me.para_box, "Press F1 for info on parameters for each prediction")
        Me.tt.SetToolTip(Me.cbBOD_SA, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")
        Me.tt.SetToolTip(Me.cbNH3_SA, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")
        Me.tt.SetToolTip(Me.cbTP_SA, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")
        Me.tt.SetToolTip(Me.cbTN_SA, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")
        Me.tt.SetToolTip(Me.cbDO_SA, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")
        Me.tt.SetToolTip(Me.cbPH_SA, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")
        Me.tt.SetToolTip(Me.cbT_SA, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")
        Me.tt.SetToolTip(Me.cbTrans_SA, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")
        Me.tt.SetToolTip(Me.cbCOD_SA, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")
        Me.tt.SetToolTip(Me.gbScenario, "Increase in the model means {100+Entry}% and decrease in the model means {100 -Entry}%  ")

    End Sub
    
    ' On resize action, resize the ZedGraphControl to fill most of the Form, with a small
    ' margin around the outside
    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        SetSize()
    End Sub
    Private Sub SetSize()
        Dim loc As New Point(10, 10)
        zg1.Location = loc
        ' Leave a small margin around the outside of the control
        Dim size As New Size(Me.ClientRectangle.Width - 20, Me.ClientRectangle.Height - 20)
        zg1.Size = size
    End Sub

    'Display customized tooltips when the mouse hovers over a point
    Private Function MyPointValueEvent(ByVal control As ZedGraphControl, _
            ByVal pane As GraphPane, ByVal curve As CurveItem, _
            ByVal iPt As Integer) As String

        ' Get the PointPair that is under the mouse
        Dim pt As PointPair = curve(iPt)

        Return curve.Label.Text + " is " + pt.Y.ToString("f2") + " units at " + pt.X.ToString("f1") + " days"
    End Function

    ' Customize the context menu by adding a new item to the end of the menu
    Private Sub MyContextMenuBuilder(ByVal control As ZedGraphControl, _
               ByVal menu As ContextMenuStrip, ByVal mousePt As Point, _
               ByVal objState As ZedGraphControl.ContextMenuObjectState)

        Dim item As New ToolStripMenuItem
        item.Name = "add-beta"
        item.Tag = "add-beta"
        item.Text = "Add a new Beta Point"
        AddHandler item.Click, AddressOf Me.AddBetaPoint

        menu.Items.Add(item)
    End Sub

    ' Handle the "Add New Beta Point" context menu item.  This finds the curve with
    ' the CurveItem.Label = "Beta", and adds a new point to it.
    Private Sub AddBetaPoint(ByVal sender As Object, ByVal args As EventArgs)
        ' Get a reference to the "Beta" curve PointPairList
        'Dim x As Double, y As Double

        'Dim ip As IPointListEdit = zg1.GraphPane.CurveList("Beta").Points

        'If (Not IsNothing(ip)) Then
        '    x = ip.Count * 5.0
        '    y = Math.Sin(ip.Count * Math.PI / 15.0) * 16.0 * 13.5
        '    ip.Add(x, y)
        '    zg1.AxisChange()
        '    zg1.Refresh()
        'End If
    End Sub

    Private Sub zg1_ZoomEvent(ByVal control As ZedGraphControl, ByVal oldState As ZoomState, _
            ByVal newState As ZoomState)
        'Here we get notification everytime the user zooms
    End Sub

    Private Sub zg1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Public Function CopyDataGridColumn(ByVal data_grid_src As DataGridView, ByVal data_grid_dest As DataGridView, _
  ByVal src_col As Integer, Optional ByVal dest_col As Integer = 0, Optional ByVal auto_add_col As Boolean = False, _
  Optional ByVal header_text As String = "Header Text") As Integer

        If auto_add_col = True Then

            data_grid_dest.ColumnCount = data_grid_dest.ColumnCount + 1
            dest_col = data_grid_dest.Columns.Count - 1

            ''data_grid_dest.Columns.Add("col_name" & dest_col, header_text)

            'If data_grid_dest.ColumnCount > 0 Then
            '    dest_col = data_grid_dest.Columns.Count - 1

            'Else
            '    dest_col = 0
            'End If

            'data_grid_dest.Columns.Add("col_name" & dest_col, header_text)

        Else

        End If

        Dim row_count As Integer = data_grid_src.Rows.Count

        data_grid_dest.Columns(dest_col).HeaderText = header_text

        For i = 0 To row_count - 1
            data_grid_dest.Rows.Add()
            data_grid_dest.Rows(i).Cells(dest_col).Value = data_grid_src.Rows(i).Cells(src_col).Value

        Next
        Return 1

    End Function


    Private Sub Tab_Sensitivity_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCOD_p.CheckedChanged, cbBOD_p.CheckedChanged, cbDO_p.CheckedChanged, cbPH_p.CheckedChanged, cbT_p.CheckedChanged, cbPH_p.CheckedChanged, cbTN_p.CheckedChanged, cbTP_p.CheckedChanged, cbTrans_p.CheckedChanged, cbNH3_p.CheckedChanged, cbCOD_SA.CheckedChanged, cbBOD_SA.CheckedChanged, cbDO_SA.CheckedChanged, cbPH_SA.CheckedChanged, cbT_SA.CheckedChanged, cbPH_SA.CheckedChanged, cbTN_SA.CheckedChanged, cbTP_SA.CheckedChanged, cbTrans_SA.CheckedChanged, cbNH3_SA.CheckedChanged

        'if parameters are checked 
        If cbBOD.CheckState = CheckState.Checked = True Then cbBOD_p.Checked = CheckState.Unchecked
        If cbTP.CheckState = CheckState.Checked = True Then cbTP_p.Checked = CheckState.Unchecked
        If cbNH3.CheckState = CheckState.Checked = True Then cbNH3_p.Checked = CheckState.Unchecked
        'if a Variable is checked 

        If cbBOD_p.Checked = True Then cbBOD_SA.CheckState = CheckState.Checked
        If cbCOD_p.Checked = True Then cbCOD_SA.CheckState = CheckState.Checked
        If cbDO_p.Checked = True Then cbDO_SA.CheckState = CheckState.Checked
        If cbNH3_p.Checked = True Then cbNH3_SA.CheckState = CheckState.Checked
        If cbT_p.Checked = True Then cbT_SA.CheckState = CheckState.Checked
        If cbTN_p.Checked = True Then cbTN_SA.CheckState = CheckState.Checked
        If cbTP_p.Checked = True Then cbTP_SA.CheckState = CheckState.Checked
        If cbTrans_p.Checked = True Then cbTrans_SA.CheckState = CheckState.Checked
        If cbPH_p.Checked = True Then cbPH_SA.CheckState = CheckState.Checked

        'if a variable is not selected 

        If cbBOD_p.Checked = False Then cbBOD_SA.CheckState = CheckState.Unchecked
        If cbCOD_p.Checked = False Then cbCOD_SA.CheckState = CheckState.Unchecked
        If cbDO_p.Checked = False Then cbDO_SA.CheckState = CheckState.Unchecked
        If cbNH3_p.Checked = False Then cbNH3_SA.CheckState = CheckState.Unchecked
        If cbT_p.Checked = False Then cbT_SA.CheckState = CheckState.Unchecked
        If cbTN_p.Checked = False Then cbTN_SA.CheckState = CheckState.Unchecked
        If cbTP_p.Checked = False Then cbTP_SA.CheckState = CheckState.Unchecked
        If cbTrans_p.Checked = False Then cbTrans_SA.CheckState = CheckState.Unchecked
        If cbPH_p.Checked = False Then cbPH_SA.CheckState = CheckState.Unchecked

        EnableText()
    End Sub
    Sub EnableText()
        If cbBOD_p.Checked = True Then
            tbBOD.Enabled = True
            cbBOD_SA.Checked = True
        Else
            tbBOD.Enabled = False
            cbBOD_SA.Checked = False

        End If

        If cbCOD_p.Checked = True Then
            tbCOD.Enabled = True
            cbCOD_SA.Checked = True
        Else
            tbCOD.Enabled = False
            cbCOD_SA.Checked = False

        End If

        If cbDO_p.Checked = True Then
            tbDO.Enabled = True
            cbDO_SA.Checked = True
        Else
            tbDO.Enabled = False
            cbDO_SA.Checked = False
        End If

        If cbNH3_p.Checked = True Then
            tbNH3.Enabled = True
            cbNH3_SA.Checked = True
        Else
            tbNH3.Enabled = False
            cbNH3_SA.Checked = False
        End If

        If cbT_p.Checked = True Then
            tbT.Enabled = True
            cbT_SA.Checked = True
        Else
            tbT.Enabled = False
            cbT_SA.Checked = False
        End If

        If cbTN_p.Checked = True Then
            tbTN.Enabled = True
            cbTN_SA.Checked = True

        Else
            tbTN.Enabled = False
            cbTN_SA.Checked = False

        End If

        If cbTP_p.Checked = True Then
            tbTP.Enabled = True
            cbTP_SA.Checked = True

        Else
            tbTP.Enabled = False
            cbTP_SA.Checked = False
        End If

        If cbTrans_p.Checked = True Then
            tbTrans.Enabled = True
            cbTrans_SA.Checked = True
        Else
            tbTrans.Enabled = False
            cbTrans_SA.Checked = False
        End If


        If cbPH_p.Checked = True Then
            tbPH.Enabled = True
            cbPH_SA.Checked = True
        Else
            tbPH.Enabled = False
            cbPH_SA.Checked = False
        End If
    End Sub
    Private Sub cbBOD_SA_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbBOD_SA.CheckedChanged
        If cbBOD_p.Checked = True Then cbBOD_SA.CheckState = CheckState.Checked
    End Sub


    Private Sub tbBOD_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbBOD.TextChanged
        If Not IsNumeric(tbBOD.Text) Then
            MessageBox.Show("ONLY NUMBERS ACCEPTED")
        End If
        If tbBOD.Text > 200 Or tbBOD.Text = vbEmpty Then
            MessageBox.Show("The Increase cannot be more than 200%, RETRY AGAIN!")
        End If
    End Sub

    Private Sub tbCOD_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbCOD.TextChanged
        If Not IsNumeric(tbCOD.Text) Then
            MessageBox.Show("ONLY NUMBERS ACCEPTED")
        End If
    End Sub

    Private Sub tbDO_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbDO.TextChanged
        If Not IsNumeric(tbDO.Text) Then
            MessageBox.Show("ONLY NUMBERS ACCEPTED")
        End If
    End Sub

    Private Sub tbPH_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbPH.TextChanged
        If Not IsNumeric(tbPH.Text) Then
            MessageBox.Show("ONLY NUMBERS ACCEPTED")
        End If
    End Sub

    Private Sub tbT_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbT.TextChanged
        If Not IsNumeric(tbT.Text) Then
            MessageBox.Show("ONLY NUMBERS ACCEPTED")
        End If
    End Sub

    Private Sub tbTrans_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbTrans.TextChanged
        If Not IsNumeric(tbTrans.Text) Then
            MessageBox.Show("ONLY NUMBERS ACCEPTED")
        End If

    End Sub

    Private Sub tbNH3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbNH3.TextChanged
        If Not IsNumeric(tbNH3.Text) Then
            MessageBox.Show("ONLY NUMBERS ACCEPTED")
        End If
    End Sub

    Private Sub tbTN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbTN.TextChanged
        If Not IsNumeric(tbTN.Text) Then
            MessageBox.Show("ONLY NUMBERS ACCEPTED")
        End If
    End Sub

    Private Sub tbTP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbTP.TextChanged
        If Not IsNumeric(tbTP.Text) Then
            MessageBox.Show("ONLY NUMBERS ACCEPTED")
        End If
    End Sub

    Private Sub BtStimulation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtStimulation.Click

        '---------------------------------|PERFORM CHECK BEFORE OPERATION OF SENSTIVITY ANALYSIS 
        If cbTP.Checked And cbBOD.Checked Or cbNH3.Checked And cbBOD.Checked Or cbTP.Checked And cbNH3.Checked Then
            MessageBox.Show("Only One Prediction Variable is Acceptable - Please Check Again")

        ElseIf cbBOD_p.Checked And tbBOD.Text = "" Or cbNH3_p.Checked And tbNH3.Text = "" Or cbT_p.Checked And tbT.Text = "" Or cbDO_p.Checked And tbDO.Text = "" Or cbTrans_p.Checked And tbTrans.Text = "" Or cbPH_p.Checked And tbPH.Text = "" Or cbTN_p.Checked And tbTN.Text = "" Or cbTP_p.Checked And tbTP.Text = "" Then
            MessageBox.Show("Please type in your scenario test values for variables")
            Return
        End If

        '--------------------------------------------------------------------------------------------------------------

        Try
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = dgv1.Rows.Count - 1
            Dim intmin As Integer = 0
            Dim intmax As Integer = dgv1.Rows.Count - 1
            '-----------------------------|SCENARIO TEST ANALYSIS | PREPARATION OF COMPUTATION------------------------------------
            Dim sen_col As Integer = dgv1.Columns.Count - 1
            Dim sen_colr As Integer = sen_col + 1

            dgv1.Columns.Add("Sen_colr", "scenario test Results")
            For i = 0 To dgv1.Rows.Count - 1 Step 1


                ProgressBar1.Visible = True
                'frmProgress.ProgressBar1.Value = i

                LbProgress.Visible = True
                ProgressBar1.Value = i
                LbProgress.Text = i / (intmax - intmin) * 100 & "% Complete"

                '-----------------------|SCENARIO TEST ANALYSIS | TP Algorithms and Data formating --------------------------------------
                '-----------------------{DEFINING PARAMETERS FOR TP COMPUTATION}-------------------------------------------------------

                Dim eqn_tp As Double
                Dim pH As Double = dgv1.Rows(i).Cells(0).Value
                Dim NH_3_N As Double = dgv1.Rows(i).Cells(2).Value
                Dim COD As Double = dgv1.Rows(i).Cells(1).Value
                '-----------------------------------------------------------------------------------------------------------------------
                If cbTP.Checked Then

                    If cbNH3_SA.Checked And cbPH_SA.Checked Then
                        pH = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        NH_3_N = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(2).Value
                        eqn_tp = 2.73 - (0.377 * pH) + ((0.009304) * (pH * NH_3_N)) + ((0.001186) * (pH * COD))
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_tp
                    ElseIf cbNH3_SA.Checked Then
                        NH_3_N = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(2).Value
                        eqn_tp = (0.0028025) + (0.093879) * NH_3_N
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_tp
                    ElseIf cbNH3_p.Checked And cbCOD_p.Checked Then
                        NH_3_N = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(2).Value
                        COD = 0.01 * tbCOD.Text * dgv1.Rows(i).Cells(1).Value
                        eqn_tp = 0.453 + (0.0011845) * NH_3_N * COD
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_tp

                    ElseIf cbPH_p.Checked And cbCOD_p.Checked Then
                        COD = 0.01 * tbCOD.Text * dgv1.Rows(i).Cells(1).Value
                        pH = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        eqn_tp = 0.171 + (0.0027797) * pH * COD
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_tp
                    ElseIf cbPH_p.Checked And cbNH3_p.Checked Then
                        pH = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        NH_3_N = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(2).Value
                        eqn_tp = 3.248 - 0.447 * pH + (0.013552) * pH * NH_3_N
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_tp
                    Else
                        MessageBox.Show("Error, Please Check Input Variables")
                        Return
                    End If
                End If


                '------------|SCENARIO TEST ANALYSIS | BOD Algorithms and Data formating ---------------------------------------------
                '----------------{DEFINING PARAMENTERS FOR BOD COMPUTATION}-------------------------------------------------------------

                Dim eqn_bod As Double
                Dim pH_b As Double = dgv1.Rows(i).Cells(0).Value
                Dim DO_b As Double = dgv1.Rows(i).Cells(1).Value
                Dim COD_b As Double = dgv1.Rows(i).Cells(2).Value
                Dim TP_b As Double = dgv1.Rows(i).Cells(3).Value
                Dim NH_3_N_b As Double = dgv1.Rows(i).Cells(4).Value
                Dim TN As Double = dgv1.Rows(i).Cells(5).Value
                '-------------------------------------------------------------------------------------------------------------------
                If cbBOD.Checked Then
                    If cbCOD_SA.Checked And cbNH3_SA.Checked And cbTP_SA.Checked Then
                        COD_b = 0.01 * tbCOD.Text * dgv1.Rows(i).Cells(2).Value
                        NH_3_N_b = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(4).Value
                        TP_b = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(3).Value
                        eqn_bod = 9.31452 + 0.0126097 * COD * NH_3_N_b + 6.57477 * TP_b
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_bod
                    ElseIf cbCOD_SA.Checked And cbNH3_SA.Checked Then
                        COD_b = 0.01 * tbCOD.Text * dgv1.Rows(i).Cells(2).Value
                        NH_3_N_b = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(4).Value
                        eqn_bod = 12.5032 + 0.0145457 * COD_b * NH_3_N_b
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_bod

                    ElseIf cbCOD_SA.Checked And cbNH3_SA.Checked And cbTP_SA.Checked And cbPH_SA.Checked Then
                        COD_b = 0.01 * tbCOD.Text * dgv1.Rows(i).Cells(2).Value
                        NH_3_N_b = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(4).Value
                        TP_b = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(3).Value
                        pH_b = 0.01 * tbPH.Text * dgv1.Rows(i).Cells(0).Value
                        eqn_bod = -9.39783 + 0.0125041 * COD_b * NH_3_N_b + 6.19133 * TP_b + 2.63608 * pH_b
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_bod
                    ElseIf cbCOD_SA.Checked And cbNH3_SA.Checked And cbDO_SA.Checked Then
                        COD_b = 0.01 * tbCOD.Text * dgv1.Rows(i).Cells(2).Value
                        NH_3_N_b = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(4).Value
                        DO_b = 0.01 * tbDO.Text * dgv1.Rows(i).Cells(1).Value
                        eqn_bod = 18.4417 + 0.0141608 * COD_b * NH_3_N_b - 0.0656279 * DO_b * COD_b
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_bod
                    ElseIf cbCOD_SA.Checked And cbNH3_SA.Checked And cbDO_SA.Checked And cbPH_SA.Checked Then
                        COD_b = 0.01 * tbCOD.Text * dgv1.Rows(i).Cells(2).Value
                        NH_3_N_b = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(4).Value
                        DO_b = 0.01 * tbDO.Text * dgv1.Rows(i).Cells(1).Value
                        pH_b = 0.01 * tbPH.Text * dgv1.Rows(i).Cells(0).Value
                        eqn_bod = -16.8157 + 0.0142863 * COD_b * NH_3_N_b - 0.144308 * DO_b * NH_3_N_b + 4.36124 * pH_b
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_bod

                    ElseIf cbCOD_SA.Checked And cbNH3_SA.Checked And cbDO_SA.Checked And cbTP_SA.Checked Then
                        COD_b = 0.01 * tbCOD.Text * dgv1.Rows(i).Cells(2).Value
                        NH_3_N_b = 0.01 * tbNH3.Text * dgv1.Rows(i).Cells(4).Value
                        DO_b = 0.01 * tbDO.Text * dgv1.Rows(i).Cells(1).Value
                        TP_b = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(3).Value
                        eqn_bod = 11.4174 + 0.0146564 * COD_b * NH_3_N_b + 0.851954 * DO_b * TP_b
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_bod

                    Else
                        MessageBox.Show("Error, Please Check Input Variables")
                        Return
                    End If

                End If


                '------------|SCENARIO TEST ANALYSIS | Ammonia-Nitrogen Algorithms and Data formating--------------

                Dim eqn_NH As Double
                Dim TP_a As Double = dgv1.Rows(i).Cells(0).Value
                Dim TN_a As Double = dgv1.Rows(i).Cells(1).Value
                Dim WT_a As Double = dgv1.Rows(i).Cells(2).Value
                Dim T_a As Double = dgv1.Rows(i).Cells(3).Value
                Dim DO_a As Double = dgv1.Rows(i).Cells(4).Value
                Dim COD_a As Double = dgv1.Rows(i).Cells(5).Value

                If cbNH3.Checked Then

                    If cbTP_SA.Checked And cbTN_SA.Checked And cbT_SA.Checked And cbTrans_SA.Checked Then
                        TP_a = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        TN_a = 0.01 * tbTN.Text * dgv1.Rows(i).Cells(1).Value
                        WT_a = 0.01 * tbT.Text * dgv1.Rows(i).Cells(2).Value
                        T_a = 0.01 * tbTrans.Text * dgv1.Rows(i).Cells(3).Value
                        eqn_NH = -6.94255 + 0.91888 * Sqrt((TP_a) * (TN_a)) - 304.222 * 1 / ((T_a) * (WT_a)) + 3.86231 * Sqrt((TN_a))
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_NH
                    ElseIf cbTP_SA.Checked And cbTN_SA.Checked And cbTrans_SA.Checked Then
                        TP_a = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        TN_a = 0.01 * tbTN.Text * dgv1.Rows(i).Cells(1).Value
                        T_a = 0.01 * tbTrans.Text * dgv1.Rows(i).Cells(3).Value
                        eqn_NH = -8.02138 + 0.941416 * Sqrt((TP_a) * (TN_a)) + 3.77342 * Sqrt((TN_a)) + 0.0311467 * (T_a)
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_NH
                    ElseIf cbTP_SA.Checked And cbTN_SA.Checked And cbTrans_SA.Checked And cbDO_SA.Checked Then
                        TP_a = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        TN_a = 0.01 * tbTN.Text * dgv1.Rows(i).Cells(1).Value
                        T_a = 0.01 * tbTrans.Text * dgv1.Rows(i).Cells(3).Value
                        DO_a = 0.01 * tbDO.Text * dgv1.Rows(i).Cells(4).Value
                        eqn_NH = -8.32092 + 0.952719 * Sqrt((TP_a) * (TN_a)) + 3.7428 * Sqrt((TN_a)) + 0.151695 * Sqrt((T_a) * (DO_a))
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_NH
                    ElseIf cbTP_SA.Checked And cbTN_SA.Checked And cbT_SA.Checked Then
                        TP_a = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        TN_a = 0.01 * tbTN.Text * dgv1.Rows(i).Cells(1).Value
                        WT_a = 0.01 * tbT.Text * dgv1.Rows(i).Cells(2).Value
                        eqn_NH = -6.49137 + 0.917595 * Sqrt((TP_a) * (TN_a)) - (315.105 * 1 / ((T_a) * (WT_a))) + 3.93491 * Sqrt((TN_a)) - (0.00224571 * COD_a * WT_a)
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_NH

                    ElseIf cbT_SA.Checked And cbTN_SA.Checked And cbTP_SA.Checked Then
                        WT_a = 0.01 * tbT.Text * dgv1.Rows(i).Cells(2).Value
                        TN_a = 0.01 * tbTN.Text * dgv1.Rows(i).Cells(1).Value
                        TP_a = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        eqn_NH = 2.36176 - 0.326788 * (WT_a) + 0.73978 * Sqrt((TN_a) * (WT_a)) + 0.682419 * Sqrt((TP_a) * (TN_a))
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_NH

                    ElseIf cbTP_SA.Checked And cbTN_SA.Checked And cbTrans_SA.Checked And cbT_SA.Checked And cbCOD_SA.Checked And cbDO_SA.Checked Then
                        TP_a = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        TN_a = 0.01 * tbTN.Text * dgv1.Rows(i).Cells(1).Value
                        WT_a = 0.01 * tbT.Text * dgv1.Rows(i).Cells(2).Value
                        T_a = 0.01 * tbTrans.Text * dgv1.Rows(i).Cells(3).Value
                        DO_a = 0.01 * tbDO.Text * dgv1.Rows(i).Cells(4).Value
                        COD_a = 0.01 * tbCOD.Text * dgv1.Rows(i).Cells(5).Value
                        eqn_NH = -6.21001 + 0.90686 * Sqrt((TP_a) * (TN_a)) - 312.594 * 1 / ((T_a) * (WT_a)) + 3.93318 * Sqrt((TN)) - 0.666498 * Log10((COD_a) * (DO_a))
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_NH
                    ElseIf cbTN_SA.Checked And cbT_SA.Checked And cbTP_SA.Checked Then
                        TN_a = 0.01 * tbTN.Text * dgv1.Rows(i).Cells(1).Value
                        WT_a = 0.01 * tbT.Text * dgv1.Rows(i).Cells(2).Value
                        TP_a = 0.01 * tbTP.Text * dgv1.Rows(i).Cells(0).Value
                        eqn_NH = -4.63988 + 4.65421 * Sqrt((TN_a)) - 0.0791775 * (WT_a) - 0.292869 * (1 / (TP_a))
                        dgv1.Rows(i).Cells(sen_colr).Value = eqn_NH
                    Else
                        MessageBox.Show("Error, Please Check Input Variables")
                    End If
                End If

                '------------------|SCENARIO ANALYSIS | CHECK for Selection of WQ Variable and Name column heading---
                If cbTP.Checked Then
                    dgv1.Columns(sen_colr).HeaderText = "Scenario test Tp"
                ElseIf cbBOD.Checked Then
                    dgv1.Columns(sen_colr).HeaderText = "Scenario test BOD5"
                ElseIf cbNH3.Checked Then
                    dgv1.Columns(sen_colr).HeaderText = "Scenario test NH3_N"

                    dgv1.Columns(sen_colr).HeaderText = "Scenario test TN"
                End If

                'Return

            Next
            MsgBox("Calculation Complete")
            ProgressBar1.Value = intmin
            ProgressBar1.Visible = False
            LbProgress.Visible = False
            LbProgress.Text = ""

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try


        '------------------------------------------------plot graphs, TP, BOD, Ammonium-Nitrogen -------------------------------
        If cbTP.Checked Then

            'dgv2.Columns.Clear()
            'CopyDataGridColumn(dgv1, dgv2, 4, 0, True, "Observed")
            'CopyDataGridColumn(dgv1, dgv2, 5, 0, True, "Scenario test")
            GraphLabel.Visible = True
            GraphLabel.Text = "Scenario Test Analysis for TP model"
            PlotDataGridGraph(4, 6, True)

        ElseIf cbBOD.Checked Then
            'dgv2.Columns.Clear()
            'CopyDataGridColumn(dgv1, dgv2, 5, 0, True, "Observed")
            'CopyDataGridColumn(dgv1, dgv2, 6, 0, True, "Scenario test")
            GraphLabel.Visible = True
            GraphLabel.Text = "Scenario Test Analysis for BOD5 model"
            PlotDataGridGraph(5, 7)

        ElseIf cbNH3.Checked Then
            'CopyDataGridColumn(dgv1, dgv2, 6, 0, True, "Observed")
            'CopyDataGridColumn(dgv1, dgv2, 7, 0, True, "Scenario test")
            GraphLabel.Visible = True
            GraphLabel.Text = "Scenario Test Analysis for NH3-N model"
            PlotDataGridGraph(6, 7)

            Return
        End If




    End Sub

    Private Sub BtnCheckSimulation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        If cbTP.Checked And cbBOD.Checked Or cbNH3.Checked And cbBOD.Checked Or cbTP.Checked And cbNH3.Checked Then
            MessageBox.Show("Only One Prediction Variable is Acceptable - Please Check Again")
        ElseIf cbTP.Checked Or cbBOD.Checked Or cbNH3.Checked And cbBOD_p.Checked Or cbCOD_p.Checked Or cbDO_p.Checked Or cbNH3_p.Checked Or cbT_p.Checked Or cbTN_p.Checked Or cbTP_p.Checked Or cbTrans_p.Checked Or cbPH_p.Checked Then
            MessageBox.Show("You can run simulation")
        Else
            MessageBox.Show("There is an Unknown Error - Please Check Inputs")
            Return
        End If
    End Sub
    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim Printer = New DGVPrinter
        Printer.Title = "h20tulkit Report"
        Printer.SubTitle = ""
        Printer.SubTitleFormatFlags = StringFormatFlags.LineLimit
        Printer.PageNumbers = True
        Printer.PageNumberInHeader = False
        Printer.PorportionalColumns = True
        Printer.HeaderCellAlignment = StringAlignment.Near
        Printer.Footer = "h20tulkit"
        Printer.FooterSpacing = 15
        Printer.PrintDataGridView(Me.dgv1)
        'Printer.PrintDataGridView(Me.zg1)
    End Sub

    Private Sub Button_Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Reset.Click
        cbBOD.Checked = False
        cbNH3.Checked = False
        cbTP.Checked = False
        cbTP_p.Checked = False
        cbBOD_p.Checked = False
        cbDO_p.Checked = False
        cbNH3_p.Checked = False
        cbCOD_p.Checked = False
        cbT_p.Checked = False
        cbTrans_p.Checked = False
        cbTN_p.Checked = False
        cbPH_p.Checked = False
        zg1.GraphPane.CurveList.Clear()
        txtRpt.Clear()
        dgv1.Columns.Clear()
    End Sub

    Private Sub BtnStiReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcReport.Click
        Try
            Dim intmin2 As Integer = 0
            Dim intmax2 As Integer = dgv1.Rows.Count - 1
            For i = 0 To dgv1.Rows.Count - 1

                ProgressBar1.Visible = True
                LbProgress.Visible = True
                ProgressBar1.Value = i
                LbProgress.Text = i / (intmax2 - intmin2) * 100 & "% Complete"

            Next
            Dim obs_col As Long = 0
            Dim calc_col As Long = dgv1.Columns.Count - 2
            Dim sens_col As Long = dgv1.Columns.Count - 2
            Dim row_cnt As Integer = dgv1.Rows.Count - 1
            Dim DT As DateTime = Date.Now
            Dim rpt_string As String = ""
            Dim temp_string As String = ""
            Dim temp_string2 As String = ""
            Dim av_obs As Double = 0
            Dim av_cal As Double = 0

            '-------------------------------------GENERATION OF CALCULATION REPORT -----------------------------------------------------------

            LbDate.Text = DT
            rpt_string = rpt_string & LbDate.Text & vbCrLf
            rpt_string = "h20tulkit GENERATED  REPORT" & vbCrLf
            rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
            'rpt_string = rpt_string & "-------------------------------------------------------------------" & vbCrLf
            rpt_string = rpt_string & "Environmental Surface Water Quality Standard of China | GB 3838-2002" & vbCrLf & vbCrLf
            rpt_string = rpt_string & "GRADES        I        II            III         IV       V" & vbCrLf
            rpt_string = rpt_string & "BOD5      <=  3        3             4           6        10" & vbCrLf
            rpt_string = rpt_string & "NH3-N     <=  0.15     0.5           1.0         1.5      2.0" & vbCrLf
            rpt_string = rpt_string & "Total TP  <=  0.12     0.10          0.20        0.30     0.40" & vbCrLf & vbCrLf
            rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
            rpt_string = rpt_string & "The observed data, predictions of the models and the Scenario test analysis were compared to the GB 3838-2002.  "
            rpt_string = rpt_string & "This standard provided a guide for making preliminary explanation to the measurements, the calculations and the Scenario test analysis.  "
            rpt_string = rpt_string & "However, the intepretations and explanations in this report are highly subjective and proper reconnaissance survey of the data collection techniques and environmental conditions in the area under study are critical to making the best judgment.  "
            rpt_string = rpt_string & "It must be noted that, the models used for the calculations are purely empirical, therefore, users are advised to be caustious as to detailed biological, chemical and physical processes have not been used in the development of the models.  "
            rpt_string = rpt_string & "Models were developed using Multi-Linear Regression based on USGS Virtual Beach Tool (2012).  " & vbCrLf
            rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf & vbCrLf

            '----------------------------BOD5 CALCULATION ANALYSIS--------------------------------------------------------------------------
            If cbBOD.Checked Then
                rpt_string = rpt_string & "THE BOD5 CALCULATION SUMMARY REPORT" & vbCrLf
                rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
                For j = 0 To row_cnt
                    av_cal += dgv1.Rows(j).Cells(6).Value
                    av_obs = av_obs + dgv1.Rows(j).Cells(5).Value
                    sens_col += dgv1.Rows(j).Cells(7).Value

                Next
                av_cal = av_cal / row_cnt
                av_obs = av_obs / row_cnt
                sens_col = sens_col / row_cnt

                If av_cal > av_obs And av_cal > 4 Or sens_col > av_obs And av_cal > 4 Then
                    rpt_string += "The  CALCULATED Average " & "{" & av_cal & "}" & " values are HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the water quality is in the grade column III. This means water quality is not suitable for potable uses" & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between OBSERVED and CALCULATED" & vbCrLf
                    rpt_string += "*******************************************************************************************************" & vbCrLf
                    rpt_string += "The  SCENARIO TEST RESULTS Average " & "{" & sens_col & "}" & " values are HIGHER than the observed" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the water quality is in the grade column III. This means water quality is not suitable for potable uses" & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between OBSERVED and CALCULATED" & vbCrLf
                Else
                    rpt_string += "The  CALCULATED average" & "{" & av_cal & "}" & " values were LOWER than the  OBSERVED average" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the nominal water quality is below grade III [GB3838-2002] indicating that water maybe suitable for potable uses." & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between OBSERVED and CALCULATED" & vbCrLf
                    rpt_string += "*******************************************************************************************************" & vbCrLf
                    rpt_string += "The  SCENARIO TEST RESULTS average" & "{" & sens_col & "}" & " values were LOWER than the  OBSERVED average" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the nominal water quality is below grade III [GB3838-2002] indicating that water maybe suitable for potable uses." & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between SCENARIO RUN and CALCULATED RESULTS" & vbCrLf
                    rpt_string += "*******************************************************************************************************" & vbCrLf
                End If
                For i = 0 To row_cnt

                    If dgv1.Rows(i).Cells(6).Value > dgv1.Rows(i).Cells(5).Value Then
                        temp_string = "The calculated " & "{" & dgv1.Rows(i).Cells(6).Value & "}" & "  is greater than the observed " & "{" & dgv1.Rows(i).Cells(5).Value & "}" & vbCrLf

                    Else
                        temp_string = "The calculated  " & "{" & dgv1.Rows(i).Cells(6).Value & "}" & " is less than the observed " & "{" & dgv1.Rows(i).Cells(5).Value & "}" & vbCrLf

                    End If
                    rpt_string = rpt_string & "Row ==> (" & i & ") " & temp_string

                Next
                rpt_string += "*******************************************************************************************************" & vbCrLf

                For i = 0 To row_cnt

                    If dgv1.Rows(i).Cells(7).Value > dgv1.Rows(i).Cells(5).Value Then
                        temp_string2 = "The scenario  calculated " & "{" & dgv1.Rows(i).Cells(7).Value & "}" & "  is greater than the observed " & "{" & dgv1.Rows(i).Cells(5).Value & "}" & vbCrLf
                    Else
                        temp_string2 = "The scenario  calculated  " & "{" & dgv1.Rows(i).Cells(7).Value & "}" & " is less than the observed " & "{" & dgv1.Rows(i).Cells(5).Value & "}" & vbCrLf
                    End If
                    rpt_string = rpt_string & "Row ==> (" & i & ") " & temp_string2
                Next
                rpt_string += "*******************************************************************************************************" & vbCrLf

                '----------------------------TP CALCULATION ANALYSIS--------------------------------------------------------------------------


            ElseIf cbTP.Checked Then

                rpt_string = rpt_string & "THE TP CALCULATION SUMMARY REPORT" & vbCrLf
                rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
                For j = 0 To row_cnt
                    av_cal += dgv1.Rows(j).Cells(5).Value
                    av_obs = av_obs + dgv1.Rows(j).Cells(4).Value
                    sens_col += dgv1.Rows(j).Cells(6).Value

                Next
                av_cal = av_cal / row_cnt
                av_obs = av_obs / row_cnt
                sens_col = sens_col / row_cnt

                If av_cal > av_obs And av_cal > 0.2 Or sens_col > av_obs And av_cal > 0.2 Then
                    rpt_string += "The  CALCULATED Average " & "{" & av_cal & "}" & " values are HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the water quality is in the grade column III. This means water quality is not suitable for potable uses" & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between OBSERVED and CALCULATED" & vbCrLf
                    rpt_string += "*******************************************************************************************************" & vbCrLf
                    rpt_string += "The  SCENARIO RESULTS Average " & "{" & sens_col & "}" & " values are HIGHER than the observed" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the water quality is in the grade column III. This means water quality is not suitable for potable uses" & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between OBSERVED and CALCULATED" & vbCrLf
                    rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
                    rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
                Else
                    rpt_string += "The  CALCULATED average" & "{" & av_cal & "}" & " values were LOWER than the  OBSERVED average" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the nominal water quality is below grade III [GB3838-2002] indicating that water maybe suitable for potable uses." & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between OBSERVED and CALCULATED" & vbCrLf
                    rpt_string += "*******************************************************************************************************" & vbCrLf
                    rpt_string += "The  SCENARIO RESULTS average" & "{" & sens_col & "}" & " values were LOWER than the  OBSERVED average" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the nominal water quality is below grade III [GB3838-2002] indicating that water maybe suitable for potable uses." & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between SCENARIO RUN and CALCULATED RESULTS" & vbCrLf
                    rpt_string += "*******************************************************************************************************" & vbCrLf
                    rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
                End If
                For i = 0 To row_cnt

                    If dgv1.Rows(i).Cells(5).Value > dgv1.Rows(i).Cells(4).Value Then
                        temp_string = "The calculated " & "{" & dgv1.Rows(i).Cells(6).Value & "}" & "  is greater than the observed " & "{" & dgv1.Rows(i).Cells(4).Value & "}" & vbCrLf

                    Else
                        temp_string = "The calculated  " & "{" & dgv1.Rows(i).Cells(5).Value & "}" & " is less than the observed " & "{" & dgv1.Rows(i).Cells(4).Value & "}" & vbCrLf

                    End If
                    rpt_string = rpt_string & "Row ==> (" & i & ") " & temp_string

                Next
                rpt_string += "*******************************************************************************************************" & vbCrLf

                For i = 0 To row_cnt

                    If dgv1.Rows(i).Cells(6).Value > dgv1.Rows(i).Cells(4).Value Then
                        temp_string2 = "The scenario test calculated " & "{" & dgv1.Rows(i).Cells(6).Value & "}" & "  is greater than the observed " & "{" & dgv1.Rows(i).Cells(4).Value & "}" & vbCrLf
                    Else
                        temp_string2 = "The scenario test calculated  " & "{" & dgv1.Rows(i).Cells(6).Value & "}" & " is less than the observed " & "{" & dgv1.Rows(i).Cells(4).Value & "}" & vbCrLf
                    End If
                    rpt_string = rpt_string & "Row ==> (" & i & ") " & temp_string2
                Next
                rpt_string += "*******************************************************************************************************" & vbCrLf


            ElseIf cbNH3.Checked Then

                rpt_string = rpt_string & "THE NH3-N CALCULATION SUMMARY REPORT" & vbCrLf
                rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
                For j = 0 To row_cnt
                    av_cal += dgv1.Rows(j).Cells(7).Value
                    av_obs = av_obs + dgv1.Rows(j).Cells(6).Value
                    sens_col += dgv1.Rows(j).Cells(8).Value

                Next
                av_cal = av_cal / row_cnt
                av_obs = av_obs / row_cnt
                sens_col = sens_col / row_cnt

                If av_cal > av_obs And av_cal > 1.0 Or sens_col > av_obs And av_cal > 1.0 Then
                    rpt_string += "The  CALCULATED Average " & "{" & av_cal & "}" & " values are HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the water quality is in the grade column III. This means water quality is not suitable for potable uses" & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between OBSERVED and CALCULATED" & vbCrLf
                    rpt_string += "*******************************************************************************************************" & vbCrLf
                    rpt_string += "The  SCENARIO TEST RESULTS Average " & "{" & sens_col & "}" & " values are HIGHER than the observed" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the water quality is in the grade column III. This means water quality is not suitable for potable uses" & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between OBSERVED and CALCULATED" & vbCrLf
                    rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
                    rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
                Else
                    rpt_string += "The  CALCULATED average" & "{" & av_cal & "}" & " values were LOWER than the  OBSERVED average" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the nominal water quality is below grade III [GB3838-2002] indicating that water maybe suitable for potable uses." & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between OBSERVED and CALCULATED" & vbCrLf
                    rpt_string += "*******************************************************************************************************" & vbCrLf
                    rpt_string += "The  SCENARIO TEST RESULTS  average" & "{" & sens_col & "}" & " values were LOWER than the  OBSERVED average" & "{" & av_obs & "}" & vbCrLf
                    rpt_string += "The calculated values indicate that the nominal water quality is below grade III [GB3838-2002] indicating that water maybe suitable for potable uses." & vbCrLf
                    rpt_string += "Further investigation is necessary to ascertain these predictions since there is a variation between SCENARIO RUN and CALCULATED RESULTS" & vbCrLf
                    rpt_string += "*******************************************************************************************************" & vbCrLf
                    rpt_string = rpt_string & "*******************************************************************************************************" & vbCrLf
                End If
                For i = 0 To row_cnt

                    If dgv1.Rows(i).Cells(7).Value > dgv1.Rows(i).Cells(6).Value Then
                        temp_string = "The calculated " & "{" & dgv1.Rows(i).Cells(7).Value & "}" & "  is greater than the observed " & "{" & dgv1.Rows(i).Cells(6).Value & "}" & vbCrLf

                    Else
                        temp_string = "The calculated  " & "{" & dgv1.Rows(i).Cells(7).Value & "}" & " is less than the observed " & "{" & dgv1.Rows(i).Cells(6).Value & "}" & vbCrLf

                    End If
                    rpt_string = rpt_string & "Row ==> (" & i & ") " & temp_string

                Next
                rpt_string += "*******************************************************************************************************" & vbCrLf

                For i = 0 To row_cnt

                    If dgv1.Rows(i).Cells(8).Value > dgv1.Rows(i).Cells(6).Value Then
                        temp_string2 = "The scenario test calculated " & "{" & dgv1.Rows(i).Cells(8).Value & "}" & "  is greater than the observed " & "{" & dgv1.Rows(i).Cells(6).Value & "}" & vbCrLf
                    Else
                        temp_string2 = "The scenario test calculated  " & "{" & dgv1.Rows(i).Cells(8).Value & "}" & " is less than the observed " & "{" & dgv1.Rows(i).Cells(6).Value & "}" & vbCrLf
                    End If
                    rpt_string = rpt_string & "Row ==> (" & i & ") " & temp_string2
                Next
                rpt_string += "*******************************************************************************************************" & vbCrLf

            Else
                MessageBox.Show("Error, please select prediction variable")
            End If
            txtRpt.Text = rpt_string
            txtRpt.TextAlign = HorizontalAlignment.Left
            MsgBox("Calculation Complete")
            ProgressBar1.Value = intmin2
            ProgressBar1.Visible = False
            LbProgress.Visible = False
            LbProgress.Text = ""
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LbProgress.Click

    End Sub

    Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btnSensReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim intmin2 As Integer = 0
        Dim intmax2 As Integer = dgv1.Rows.Count - 1
        For i = 0 To dgv1.Rows.Count - 1

            ProgressBar1.Visible = True
            LbProgress.Visible = True
            ProgressBar1.Value = i
            LbProgress.Text = i / (intmax2 - intmin2) * 100 & "% Complete"

        Next

        Dim obs_col As Integer = 0
        Dim calc_col As Integer = dgv1.Columns.Count - 2
        Dim sens_col As Integer = dgv1.Columns.Count - 1
        Dim row_cnt As Integer = dgv1.Rows.Count - 1
        Dim DT As DateTime = Date.Now
        Dim rpt_string As String = ""
        Dim temp_string As String = ""
        Dim av_obs As Double = 0
        Dim av_cal As Double = 0
        For j = 0 To row_cnt
            av_cal += dgv1.Rows(j).Cells(6).Value
            av_obs = av_obs + dgv1.Rows(j).Cells(5).Value

        Next

        av_cal = av_cal / row_cnt
        av_obs = av_obs / row_cnt


        '-------------------------------------GENERATION OF SCENARIO ANALYSIS REPORT -----------------------------------------------------------

        rpt_string = "h20tulkit GENERATED SCENARIO ANALYSIS REPORT" & vbCrLf

        ''---------------------------TP Model Scenario Analysis--------------------------------------------------


        If cbTP.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbPH_p.Checked And av_cal > av_obs Then
            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value

            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt

            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------" & vbCrLf
            rpt_string = "The change in the NH3-N " & "{" & tbNH3.Text & "}" & "," & "COD" & "{" & tbCOD.Text & "}" & "and" & "pH" & "{" & tbPH.Text & "}" & vbCrLf
            rpt_string = "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
            rpt_string = "The change in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"

        ElseIf cbTP.Checked And cbNH3_p.Checked And av_cal > av_obs Then
            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value

            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt

            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------" & vbCrLf
            rpt_string = "The change in the NH3-N " & "{" & tbNH3.Text & "}" & vbCrLf
            rpt_string += "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
            rpt_string = "The change in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"


        ElseIf cbTP.Checked And cbNH3_p.Checked And cbCOD_p.Checked And av_cal > av_obs Then

            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value

            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt
            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------" & vbCrLf
            rpt_string = "The change in the NH3-N " & "{" & tbNH3.Text & "}" & "," & "COD" & "{" & tbCOD.Text & "}" & vbCrLf
            rpt_string += "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
            rpt_string = "The change in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"


        ElseIf cbTP.Checked And cbPH_p.Checked And cbCOD_p.Checked And av_cal > av_obs Then
            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value

            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt
            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------" & vbCrLf
            rpt_string = "The change in the COD" & "{" & tbCOD.Text & "}" & "and" & "pH" & "{" & tbPH.Text & "}" & vbCrLf
            rpt_string += "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
            rpt_string = "The change in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"


        ElseIf cbTP.Checked And cbPH_p.Checked And cbNH3_p.Checked And av_cal > av_obs Then
            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value
            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt
            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------" & vbCrLf
            rpt_string = "The change in the NH3-N " & "{" & tbNH3.Text & "}" & "," & "TP" & "{" & tbTP.Text & "}" & "and" & "pH" & "{" & tbPH.Text & "}" & vbCrLf
            rpt_string += "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
            rpt_string = "The change in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"


            '------------------------------------------When Av_Obs>Av_Cal


        ElseIf cbCOD_p.Checked And cbNH3_p.Checked And cbPH_p.Checked And av_cal < av_obs Then
            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value

            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt

            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------" & vbCrLf
            rpt_string = "The change in the NH3-N " & "{" & tbNH3.Text & "}" & "," & "COD" & "{" & tbCOD.Text & "}" & "and" & "pH" & "{" & tbPH.Text & "}" & vbCrLf
            rpt_string += "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
            rpt_string = "The change in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"

        ElseIf cbTP.Checked And cbNH3_p.Checked And av_cal < av_obs Then
            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value

            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt

            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------" & vbCrLf
            rpt_string = "The change in the NH3-N " & "{" & tbNH3.Text & "}" & vbCrLf
            rpt_string += "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
            rpt_string = "The change in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"


        ElseIf cbTP.Checked And cbNH3_p.Checked And cbCOD_p.Checked And av_cal < av_obs Then

            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value

            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt
            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------"
            rpt_string += "The change in the NH3-N " & "{" & tbNH3.Text & "}" & "," & "COD" & "{" & tbCOD.Text & "}"
            rpt_string += "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}"
            rpt_string += "The change in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"


        ElseIf cbTP.Checked And cbPH_p.Checked And cbCOD_p.Checked And av_cal < av_obs Then
            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value

            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt
            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------" & vbCrLf
            rpt_string = "The change in the COD" & "{" & tbCOD.Text & "}" & "and" & "pH" & "{" & tbPH.Text & "}" & vbCrLf
            rpt_string += "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
            rpt_string = "The change in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"


        ElseIf cbTP.Checked And cbPH_p.Checked And cbNH3_p.Checked And av_cal < av_obs Then
            For j = 0 To row_cnt
                av_cal += dgv1.Rows(j).Cells(5).Value
                av_obs = av_obs + dgv1.Rows(j).Cells(4).Value
            Next
            av_cal = av_cal / row_cnt
            av_obs = av_obs / row_cnt
            rpt_string = rpt_string & "---------------------------TP Model Scenario Analysis--------------------------------------------------" & vbCrLf
            rpt_string = "The change in the NH3-N " & "{" & tbNH3.Text & "}" & "," & "TP" & "{" & tbTP.Text & "}" & "and" & "pH" & "{" & tbPH.Text & "}" & vbCrLf
            rpt_string += "The  CALCULATED AVERAGE " & "{" & av_cal & "}" & " value is  HIGHER than the OBSERVED" & "{" & av_obs & "}" & vbCrLf
            rpt_string = "The changet in the concentrations of EACH VARIABLE affected a GENERAL INCREASE IN CALCULATED WATER QUALITY CONCENTRATION"
        Else
            MsgBox("Calculation Complete")
            Return
        End If

        MsgBox("Calculation Complete")
        ProgressBar1.Value = intmin2
        ProgressBar1.Visible = False
        LbProgress.Visible = False
        LbProgress.Text = ""

    End Sub

    Private Sub txtRpt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRpt.TextChanged

    End Sub

    Private Sub TxtRpt2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub GraphToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        zg1.Show()


    End Sub

    Private Sub prt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub PrintTableToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintTableToolStripMenuItem.Click
        Dim Printer = New DGVPrinter
        Printer.Title = "h20 Analysis Report"
        Printer.SubTitle = ""
        Printer.SubTitleFormatFlags = StringFormatFlags.LineLimit
        Printer.PageNumbers = True
        Printer.PageNumberInHeader = False
        Printer.PorportionalColumns = True
        Printer.HeaderCellAlignment = StringAlignment.Near
        Printer.Footer = "h20tulkit"
        Printer.FooterSpacing = 8
        Printer.PrintPreviewDataGridView(Me.dgv1)
        'Printer.PrintDataGridView(Me.dgv1)
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub PictureBox1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Load_file_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Load_file.Click

        Dim OpenFileDialog As New OpenFileDialog
        If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            '    'Dim Pathname As String = OpenFileDialog.FileName
            Dim sFileName As String = OpenFileDialog.FileName
            Dim connectionStringTemplate As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"""
            Dim connectionString As String = String.Format(connectionStringTemplate, sFileName)
            Dim sqlSelect As String = "SELECT * FROM [Sheet1$];"
            ' Load the Excel worksheet into a DataTable
            Dim workbook As DataSet = New DataSet()
            Dim excelAdapter As System.Data.Common.DataAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlSelect, connectionString)
            Try
                excelAdapter.Fill(workbook)
                Dim worksheet As DataTable = workbook.Tables(0)
                dgv1.DataSource = worksheet
                dgv1.Columns.Add("calc_col", "")
                Me.Refresh()
            Catch
                'MANAGE EXCEPTION
            End Try
            dgv1.DefaultCellStyle.Format = "N"
        End If
    End Sub

    Private Sub Prt_fast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Prt_fast.Click


        Dim Printer = New DGVPrinter
        Printer.Title = "h20 Analysis Report"
        Printer.SubTitle = ""
        Printer.SubTitleFormatFlags = StringFormatFlags.LineLimit
        Printer.PageNumbers = True
        Printer.PageNumberInHeader = False
        Printer.PorportionalColumns = True
        Printer.HeaderCellAlignment = StringAlignment.Near
        Printer.Footer = "h20tulkit"
        Printer.FooterSpacing = 8
        Printer.PrintPreviewDataGridView(Me.dgv1)
    End Sub

    Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reset.Click

        cbBOD.Checked = False
        cbNH3.Checked = False
        cbTP.Checked = False
        cbTP_p.Checked = False
        cbBOD_p.Checked = False
        cbDO_p.Checked = False
        cbNH3_p.Checked = False
        cbCOD_p.Checked = False
        cbT_p.Checked = False
        cbTrans_p.Checked = False
        cbTN_p.Checked = False
        cbPH_p.Checked = False
        zg1.GraphPane.CurveList.Clear()
        txtRpt.Clear()
        dgv1.Columns.Clear()
    End Sub

    Private Sub calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculate.Click

        If dgv1.CurrentCell Is Nothing Then
            MessageBox.Show("Import your excel data first")
            Return
        End If

        If cbBOD.Checked = False And cbTP.Checked = False And cbNH3.Checked = False Then
            MessageBox.Show("Error, select prediction variable")
            Return
        ElseIf cbBOD.Checked = True And cbTP.Checked = True And cbNH3.Checked = False Then
            MessageBox.Show("Error, select only ONE prediction variable")
            Return
        ElseIf cbBOD.Checked = False And cbTP.Checked = True And cbNH3.Checked = True Then
            MessageBox.Show("Error, select only ONE prediction variable")
            Return
        ElseIf cbBOD.Checked = True And cbTP.Checked = False And cbNH3.Checked = True Then
            MessageBox.Show("Error, select only ONE prediction variable")
            Return
        End If

        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = dgv1.Rows.Count - 1

        'frmProgress.ProgressBar1.Minimum = 0
        'frmProgress.ProgressBar1.Maximum = dgv1.Rows.Count - 1

        'frmProgress.Show()

        Dim min As Integer = 0
        Dim max As Integer = dgv1.Rows.Count - 1
        Try

            '-----------------------------PREPARATION OF COMPUTATION------------------------------------
            Dim calc_col As Integer = dgv1.Columns.Count - 1
            For i = 0 To dgv1.Rows.Count - 1 Step 1

                '-------------------------PROGRESS/STATUS BAR----------------------------------
                ProgressBar1.Visible = True
                'frmProgress.ProgressBar1.Value = i
                LbProgress.Visible = True
                ProgressBar1.Value = i
                LbProgress.Text = i / (max - min) * 100 & "% Complete"

                '-------------------TP Algorithms and Data formating --------------------------------------
                Dim eqn As Double
                Dim pH As Double = dgv1.Rows(i).Cells(0).Value
                Dim COD As Double = dgv1.Rows(i).Cells(1).Value
                Dim NH_3_N As Double = dgv1.Rows(i).Cells(2).Value


                '------------BOD Algorithms and Data formating ---------------------------------------------
                Dim eqn_1 As Double
                Dim pH_b As Double = dgv1.Rows(i).Cells(0).Value
                Dim DO_b As Double = dgv1.Rows(i).Cells(1).Value
                Dim COD_b As Double = dgv1.Rows(i).Cells(2).Value
                Dim TP_b As Double = dgv1.Rows(i).Cells(3).Value
                Dim NH_3_N_b As Double = dgv1.Rows(i).Cells(4).Value
                'Dim TN As Double = dgv1.Rows(i).Cells(5).Value

                '------------Ammonia-Nitrogen Algorithms and Data formating-------------------------------
                Dim eqn_2 As Double
                Dim TP_a As Double = dgv1.Rows(i).Cells(0).Value
                Dim TN_a As Double = dgv1.Rows(i).Cells(1).Value
                Dim WT_a As Double = dgv1.Rows(i).Cells(2).Value
                Dim T_a As Double = dgv1.Rows(i).Cells(3).Value
                Dim DO_a As Double = dgv1.Rows(i).Cells(4).Value
                Dim COD_a As Double = dgv1.Rows(i).Cells(5).Value

                '------------------CHECK for Selection of WQ Variable and Name column heading--------------
                If cbTP.Checked Then
                    dgv1.Columns(calc_col).HeaderText = "Calc Tp"
                ElseIf cbBOD.Checked Then
                    dgv1.Columns(calc_col).HeaderText = "Calc BOD5"
                ElseIf cbNH3.Checked Then
                    dgv1.Columns(calc_col).HeaderText = "Calc NH3_N"
                    dgv1.Columns(calc_col).HeaderText = "Calc TN"
                End If

                '-------------------------------CALCULATION FOR TP PREDICTION----------------------

                If cbTP.Checked And cbNH3_p.Checked And cbCOD_p.Checked And cbPH_p.Checked Then
                    eqn = 2.73 - (0.377 * pH) + ((0.009304) * (pH * NH_3_N)) + ((0.001186) * (pH * COD))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbNH3_p.Checked Then
                    eqn = (0.0028025) + (0.093879) * NH_3_N
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbNH3_p.Checked And cbCOD_p.Checked Then

                    eqn = 0.453 + (0.0011845) * NH_3_N * COD
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbPH_p.Checked And cbCOD_p.Checked Then

                    eqn = 0.171 + (0.0027797) * pH * COD
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbPH_p.Checked And cbNH3_p.Checked Then
                    eqn = 3.348 - 0.447 * pH + (0.013552) * pH * NH_3_N
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                    '------------------------------CALCULATION FOR BOD PREDICTION-------------------------------------

                ElseIf cbBOD.Checked And cbBOD_p.Checked And cbNH3_p.Checked And cbTP_p.Checked Then
                    eqn_1 = 9.31452 + 0.0126097 * COD * NH_3_N_b + 6.57477 * TP_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked Then
                    eqn_1 = 12.5032 + 0.0145457 * COD_b * NH_3_N_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1

                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbTP_p.Checked And cbPH_p.Checked Then
                    eqn_1 = -9.39783 + 0.0125041 * COD_b * NH_3_N_b + 6.19133 * TP_b + 2.63608 * pH_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1

                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbDO_p.Checked Then
                    eqn_1 = 18.4417 + 0.0141608 * COD_b * NH_3_N_b - 0.0656279 * DO_b * COD_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                ElseIf cbBOD.Checked And cbBOD_p.Checked And cbNH3_p.Checked And cbDO_p.Checked And cbPH_p.Checked Then
                    eqn_1 = -16.8157 + 0.0142863 * COD_b * NH_3_N_b - 0.144308 * DO_b * NH_3_N_b + 4.36124 * pH_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbDO_p.Checked And cbTP_p.Checked Then
                    eqn_1 = 11.4174 + 0.0146564 * COD_b * NH_3_N_b + 0.851954 * DO_b * TP_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                    '--------------------------Calculation of Ammonia-Nitrogen Prediction-----------
                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbT_p.Checked And cbTrans_p.Checked Then
                    eqn_2 = -6.94255 + 0.91888 * Sqrt((TP_a) * (TN_a)) - 304.222 * 1 / ((T_a) * (WT_a)) + 3.86231 * Sqrt((TN_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbTrans_p.Checked Then
                    eqn_2 = -8.02138 + 0.941416 * Sqrt((TP_a) * (TN_a)) + 3.77342 * Sqrt((TN_a)) + 0.0311467 * (T_a)
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbTP_p.Checked And cbTN_p.Checked And cbTrans_p.Checked And cbDO_p.Checked Then
                    eqn_2 = -8.32092 + 0.952719 * Sqrt((TP_a) * (TN_a)) + 3.7428 * Sqrt((TN_a)) + 0.151695 * Sqrt((T_a) * (DO_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbT_p.Checked Then
                    eqn_2 = -6.49137 + 0.917595 * Sqrt((TP_a) * (TN_a)) - 315.105 * 1 / ((T_a) * (WT_a) + 3.93491 * Sqrt((TN_a)) - (0.00224571 * COD_a * WT_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2


                ElseIf cbNH3.Checked And cbT_p.Checked And cbTN_p.Checked And cbTP_p.Checked Then
                    eqn_2 = 2.36176 - 0.326788 * (WT_a) + 0.73978 * Sqrt((TN_a) * (WT_a)) + 0.682419 * Sqrt((TP_a) * (TN_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbTrans_p.Checked And cbT_p.Checked And cbCOD_p.Checked And cbDO_p.Checked Then
                    eqn_2 = -6.21001 + 0.90686 * Sqrt((TP_a) * (TN_a)) - 312.594 * 1 / ((T_a) * (WT_a)) + 3.93318 * Sqrt((TN_a)) - 0.666498 * Log10((COD_a) * (DO_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTN_p.Checked And cbT_p.Checked And cbTP_p.Checked Then
                    eqn_2 = -4.63988 + 4.65421 * Sqrt((TN_a)) - 0.0791775 * (WT_a) - 0.292869 * 1 / (TP_a)
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                    '-----------------Check for ERRORS in COMPUTATION -------------------------------
                Else
                    MessageBox.Show("There are no computations option for selected variables or ensure that you have selected your parameters!")
                    Return
                End If
                'Return
            Next
            MsgBox("Calculation Complete")
            ProgressBar1.Value = min
            ProgressBar1.Visible = False
            LbProgress.Visible = False
            LbProgress.Text = ""
            frmProgress.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        '------------------------------------------------plot graphs, TP, BOD, TN, COD, Ammonium-Nitrogen -------------------------------
        If cbTP.Checked Then
            GraphLabel.Visible = True
            GraphLabel.Text = "Prediction of TP Model"
            PlotDataGridGraph(4, 5, True)
            'CopyDataGridColumn(dgv1, dgv2, 4, 0, True, "Obs")
            'CopyDataGridColumn(dgv1, dgv2, 5, 0, True, "Calc")
        ElseIf cbBOD.Checked Then
            GraphLabel.Visible = True
            GraphLabel.Text = "Prediction of BOD Model"
            PlotDataGridGraph(5, 6)
            'CopyDataGridColumn(dgv1, dgv2, 5, 0, True, "Obs")
            'CopyDataGridColumn(dgv1, dgv2, 6, 0, True, "Calc")
        ElseIf cbNH3.Checked Then
            GraphLabel.Visible = True
            GraphLabel.Text = "Prediction of NH3-N Model"
            PlotDataGridGraph(6, 7)
            'CopyDataGridColumn(dgv1, dgv2, 6, 0, True, "Obs")
            'CopyDataGridColumn(dgv1, dgv2, 7, 0, True, "Calc")
            Return
        End If
    End Sub

    Private Sub Graph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Graph.Click
        TableCon.SelectTab(1)
    End Sub

    Private Sub Model_Report_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Model_Report.Click

        PrintDocument1.PrinterSettings.Copies = 1
        PrintDocument1.Print()
    End Sub
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        e.Graphics.DrawString(txtRpt.Text, txtRpt.Font, Brushes.Blue, 100, 100)
    End Sub

    Private Sub SensitivityReportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SensitivityReportToolStripMenuItem.Click
        PrintDocument1.PrinterSettings.Copies = 1
        PrintDocument1.Print()
    End Sub

    Private Sub RunCalculationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunCalculationToolStripMenuItem.Click
        If dgv1.CurrentCell Is Nothing Then
            MessageBox.Show("Import your excel data first")
            Return
        End If

        If cbBOD.Checked = False And cbTP.Checked = False And cbNH3.Checked = False Then
            MessageBox.Show("Error, select prediction variable")
            Return
        ElseIf cbBOD.Checked = True And cbTP.Checked = True And cbNH3.Checked = False Then
            MessageBox.Show("Error, select only ONE prediction variable")
            Return
        ElseIf cbBOD.Checked = False And cbTP.Checked = True And cbNH3.Checked = True Then
            MessageBox.Show("Error, select only ONE prediction variable")
            Return
        ElseIf cbBOD.Checked = True And cbTP.Checked = False And cbNH3.Checked = True Then
            MessageBox.Show("Error, select only ONE prediction variable")
            Return
        End If

        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = dgv1.Rows.Count - 1

        'frmProgress.ProgressBar1.Minimum = 0
        'frmProgress.ProgressBar1.Maximum = dgv1.Rows.Count - 1

        'frmProgress.Show()

        Dim min As Integer = 0
        Dim max As Integer = dgv1.Rows.Count - 1
        Try
            '-----------------------------PREPARATION OF COMPUTATION------------------------------------
            Dim calc_col As Integer = dgv1.Columns.Count - 1
            For i = 0 To dgv1.Rows.Count - 1 Step 1

                '-------------------------PROGRESS/STATUS BAR----------------------------------
                ProgressBar1.Visible = True
                'frmProgress.ProgressBar1.Value = i

                LbProgress.Visible = True
                ProgressBar1.Value = i
                LbProgress.Text = i / (max - min) * 100 & "% Complete"

                '-------------------TP Algorithms and Data formating --------------------------------------
                Dim eqn As Double
                Dim pH As Double = dgv1.Rows(i).Cells(0).Value
                Dim COD As Double = dgv1.Rows(i).Cells(1).Value
                Dim NH_3_N As Double = dgv1.Rows(i).Cells(2).Value


                '------------BOD Algorithms and Data formating ---------------------------------------------
                Dim eqn_1 As Double
                Dim pH_b As Double = dgv1.Rows(i).Cells(0).Value
                Dim DO_b As Double = dgv1.Rows(i).Cells(1).Value
                Dim COD_b As Double = dgv1.Rows(i).Cells(2).Value
                Dim TP_b As Double = dgv1.Rows(i).Cells(3).Value
                Dim NH_3_N_b As Double = dgv1.Rows(i).Cells(4).Value
                'Dim TN As Double = dgv1.Rows(i).Cells(5).Value

                '------------Ammonia-Nitrogen Algorithms and Data formating--------------
                Dim eqn_2 As Double
                Dim TP_a As Double = dgv1.Rows(i).Cells(0).Value
                Dim TN_a As Double = dgv1.Rows(i).Cells(1).Value
                Dim WT_a As Double = dgv1.Rows(i).Cells(2).Value
                Dim T_a As Double = dgv1.Rows(i).Cells(3).Value
                Dim DO_a As Double = dgv1.Rows(i).Cells(4).Value
                Dim COD_a As Double = dgv1.Rows(i).Cells(5).Value

                '------------------CHECK for Selection of WQ Variable and Name column heading---
                If cbTP.Checked Then
                    dgv1.Columns(calc_col).HeaderText = "Calc Tp"
                ElseIf cbBOD.Checked Then
                    dgv1.Columns(calc_col).HeaderText = "Calc BOD5"
                ElseIf cbNH3.Checked Then
                    dgv1.Columns(calc_col).HeaderText = "Calc NH3_N"
                    dgv1.Columns(calc_col).HeaderText = "Calc TN"
                End If

                '-------------------------------CALCULATION FOR TP PREDICTION----------------------

                If cbTP.Checked And cbNH3_p.Checked And cbCOD_p.Checked And cbPH_p.Checked Then
                    eqn = 2.73 - (0.377 * pH) + ((0.009304) * (pH * NH_3_N)) + ((0.001186) * (pH * COD))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbNH3_p.Checked Then
                    eqn = (0.0028025) + (0.093879) * NH_3_N
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbNH3_p.Checked And cbCOD_p.Checked Then

                    eqn = 0.453 + (0.0011845) * NH_3_N * COD
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbPH_p.Checked And cbCOD_p.Checked Then

                    eqn = 0.171 + (0.0027797) * pH * COD
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                ElseIf cbTP.Checked And cbPH_p.Checked And cbNH3_p.Checked Then
                    eqn = 3.248 - 0.447 * pH + (0.013552) * pH * NH_3_N
                    dgv1.Rows(i).Cells(calc_col).Value = eqn

                    '------------------------------CALCULATION FOR BOD PREDICTION-------------------------------------
                ElseIf cbBOD.Checked And cbBOD_p.Checked And cbNH3_p.Checked And cbTP_p.Checked Then
                    eqn_1 = 9.31452 + 0.0126097 * COD * NH_3_N_b + 6.57477 * TP_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked Then
                    eqn_1 = 12.5032 + 0.0145457 * COD_b * NH_3_N_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1

                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbTP_p.Checked And cbPH_p.Checked Then
                    eqn_1 = -9.39783 + 0.0125041 * COD_b * NH_3_N_b + 6.19133 * TP_b + 2.63608 * pH_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1

                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbDO_p.Checked Then
                    eqn_1 = 18.4417 + 0.0141608 * COD_b * NH_3_N_b - 0.0656279 * DO_b * COD_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                ElseIf cbBOD.Checked And cbBOD_p.Checked And cbNH3_p.Checked And cbDO_p.Checked And cbPH_p.Checked Then
                    eqn_1 = -16.8157 + 0.0142863 * COD_b * NH_3_N_b - 0.144308 * DO_b * NH_3_N_b + 4.36124 * pH_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                ElseIf cbBOD.Checked And cbCOD_p.Checked And cbNH3_p.Checked And cbDO_p.Checked And cbTP_p.Checked Then
                    eqn_1 = 11.4174 + 0.0146564 * COD_b * NH_3_N_b + 0.851954 * DO_b * TP_b
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_1


                    '--------------------------Calculation of Ammonia-Nitrogen Prediction-----------
                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbT_p.Checked And cbTrans_p.Checked Then
                    eqn_2 = -6.94255 + 0.91888 * Sqrt((TP_a) * (TN_a)) - 304.222 * 1 / ((T_a) * (WT_a)) + 3.86231 * Sqrt((TN_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbTrans_p.Checked Then
                    eqn_2 = -8.02138 + 0.941416 * Sqrt((TP_a) * (TN_a)) + 3.77342 * Sqrt((TN_a)) + 0.0311467 * (T_a)
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbTP_p.Checked And cbTN_p.Checked And cbTrans_p.Checked And cbDO_p.Checked Then
                    eqn_2 = -8.32092 + 0.952719 * Sqrt((TP_a) * (TN_a)) + 3.7428 * Sqrt((TN_a)) + 0.151695 * Sqrt((T_a) * (DO_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbT_p.Checked Then
                    eqn_2 = -6.49137 + 0.917595 * Sqrt((TP_a) * (TN_a)) - (315.105 * 1 / ((T_a) * (WT_a))) + 3.93491 * Sqrt((TN_a)) - (0.00224571 * COD_a * WT_a)
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2


                ElseIf cbNH3.Checked And cbT_p.Checked And cbTN_p.Checked And cbTP_p.Checked Then
                    eqn_2 = 2.36176 - 0.326788 * (WT_a) + 0.73978 * Sqrt((TN_a) * (WT_a)) + 0.682419 * Sqrt((TP_a) * (TN_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTP_p.Checked And cbTN_p.Checked And cbTrans_p.Checked And cbT_p.Checked And cbCOD_p.Checked And cbDO_p.Checked Then
                    eqn_2 = -6.21001 + 0.90686 * Sqrt((TP_a) * (TN_a)) - 312.594 * 1 / ((T_a) * (WT_a)) + 3.93318 * Sqrt((TN_a)) - 0.666498 * Log10((COD_a) * (DO_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                ElseIf cbNH3.Checked And cbTN_p.Checked And cbT_p.Checked And cbTP_p.Checked Then
                    eqn_2 = -4.63988 + 4.65421 * Sqrt((TN_a)) - 0.0791775 * (WT_a) - 0.292869 * (1 / (TP_a))
                    dgv1.Rows(i).Cells(calc_col).Value = eqn_2

                    '-----------------Check for ERRORS in COMPUTATION -------------------------------

                Else
                    MessageBox.Show("There are no computations option for selected variables or ensure that you have selected your parameters!")
                    Return
                End If
                'Return
            Next
            MsgBox("Calculation Complete")
            ProgressBar1.Value = min
            ProgressBar1.Visible = False
            LbProgress.Visible = False
            LbProgress.Text = ""
            frmProgress.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        '------------------------------------------------plot graphs, TP, BOD, TN, COD, Ammonium-Nitrogen -------------------------------
        If cbTP.Checked Then
            GraphLabel.Visible = True
            GraphLabel.Text = "Prediction of TP Model"
            PlotDataGridGraph(4, 5, True)
            'CopyDataGridColumn(dgv1, dgv2, 4, 0, True, "Obs")
            'CopyDataGridColumn(dgv1, dgv2, 5, 0, True, "Calc")
        ElseIf cbBOD.Checked Then
            GraphLabel.Visible = True
            GraphLabel.Text = "Prediction of BOD Model"
            PlotDataGridGraph(5, 6)
            'CopyDataGridColumn(dgv1, dgv2, 5, 0, True, "Obs")
            'CopyDataGridColumn(dgv1, dgv2, 6, 0, True, "Calc")
        ElseIf cbNH3.Checked Then
            GraphLabel.Visible = True
            GraphLabel.Text = "Prediction of NH3-N Model"
            PlotDataGridGraph(6, 7)
            'CopyDataGridColumn(dgv1, dgv2, 6, 0, True, "Obs")
            'CopyDataGridColumn(dgv1, dgv2, 7, 0, True, "Calc")
            Return
        End If
    End Sub

    Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
        cbBOD.Checked = False
        cbNH3.Checked = False
        cbTP.Checked = False
        cbTP_p.Checked = False
        cbBOD_p.Checked = False
        cbDO_p.Checked = False
        cbNH3_p.Checked = False
        cbCOD_p.Checked = False
        cbT_p.Checked = False
        cbTrans_p.Checked = False
        cbTN_p.Checked = False
        cbPH_p.Checked = False
        zg1.GraphPane.CurveList.Clear()
        txtRpt.Clear()
        dgv1.Columns.Clear()
    End Sub

    Private Sub AboutH20tulkitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutH20tulkitToolStripMenuItem.Click
        System.Windows.Forms.Help.ShowHelp(Me, "Help.chm", HelpNavigator.TableOfContents)
    End Sub

    Private Sub h20tulkit_help_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles h20tulkit_help.Click

        System.Windows.Forms.Help.ShowHelp(Me, "Help.chm", HelpNavigator.TableOfContents)

    End Sub

    Private Sub cbBOD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbBOD.CheckedChanged

    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Me.Close()
    End Sub

    
    Private Sub RichTextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles website.Click
        Dim theWebSite As String
        theWebSite = "http://www.riversmodelling.com"
        Call Shell("explorer.exe " & theWebSite, vbNormalFocus)
    End Sub

   
End Class
